 var id = getvl("id");
  var  pifa=0;
 $(function(){
 	if(id!=''){
	$.ajax({
		url: urlcore + "/api/user/getadmin",
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			// console.log(data)
			if(data.success == true) {
					selectall(data.data.id);
			}else{
					if(uuuuid!=""){
						var idd=sessionStorage.getItem('id');
						// console.log(idd)
						if(idd!=""){
							selectall(idd);
              qutianjia(idd);
						}
					}else{
						alert("未登录/或登录超时");
					}
				}
		},
		error: function() {
			/* Act on the event */
			alert("error");
		}
	});
	
 	}
 });
 
 //加载显示
function selectall(userid){
 		 $.ajax({
        url: urlcore + "/api/goods/selectColorAndSizeByGoodsId?goodsId="+ userid,///goodsCar/selectGoodupdate?goodscarId="+id+"&userId="+userid
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				 // console.log(data);
				if(data.success == true) {
					var da=data.data.goods;
					var shangping='<input type="text" style="display: none;" value="'+da.id+'" id="goodsIds">'+
								'<dt>'+
								'	<img onerror="errorImg(this)" src="'+da.coverUrl+'" />'+
								'</dt>'+
								'<dd>'+
								'	<p>'+da.goodsTitle+'</p>'+ //全场满500或15件可混批
								'	<ul style="margin-top:20px" class="clearfix">'+
								'		<li>'+
								'			<span>1-'+da.tradeNumber+'件</span>'+
								'			<h6>&yen;'+da.tradePrice.toFixed(2)+'</h6>'+
								'      <input type="hidden" id="pifajiage" value="'+da.tradePrice.toFixed(2)+'">'+
								'		</li>'+
								'		<li>'+
								'			<span></span>'+//'+da.packNumber+'-'+da.tradeNumber+'件
								'			<h6></h6>'+ //&yen;'+da.tradePrice+'
								'		</li>'+
								'		<li>'+
								'			<span>'+da.packNumber+'件以上</span>'+
								'			<h6>&yen;'+da.packPrice.toFixed(2)+'</h6>'+
								'      <input type="hidden" id="jiage"  value="'+da.packPrice.toFixed(2)+'">'+
								'		</li>'+
								'</ul>'+
								'</dd>';	
                        
                pifa=da.packNumber;
                var sds=0;
                $("#shangping").append(shangping);
                var  goodsguige="";								
								$.each(data.data.list, function(y,n) {
										goodsguige+='<li class="clearfix" id="guige">'+
												'<span>'+n.colour.colourName+'</span>'+
												'<span></span>'+
												'<span>'+n.size.sizeName+'</span>'+
												'<div class="lt-rt">'+
														'<input type="text" style="display: none;" id="kuchunid" value="'+n.id+'">'+
														'<input type="text" id="shopIds"  style="display: none;" value="'+n.shopId+'">'+
														'<input type="text" style="display: none;" value="'+n.colour.leftNum+'">'+
														'<input type="button" class="minus" onclick="jianhao(this)"  value="-">' +
														'<input type="number" class="zhizhi" id="num" onchange="wenbenkuang(this)" class="result" value="0">' +
														'<input type="button" class="add" onclick="jiahao(this)" value="+">' +
													'</div>'+
													'<input type="text" style="display: none;" id="kuchuanss" value="'+n.colour.leftNum+'">'+
													'<em>库存'+n.leftNum+'件</em>'+
												'</li>';
										});  
										$("#goodsguige").append(goodsguige);
										zhijin();
				} else {
					if(data.msg != '空数据') {
						alert(data.msg)
					} else {
						$('#thiscount').text(0);
					}
				}
			},
			error: function() {
				alert("error");
			}
		});
		
}
//点击加入进货车 
function qutianjia(){
  if(uuuuid!=''){
	     var myStocks =[];
	     var myshook="";
	     var sn=0;
	     var jianshu=0;
	     myshook+='[';
           $("input[class='zhizhi']").each(function () {
           	  	 if(jianshu>=pifa){
           jine=$("#pifajiage").val();
           	 }else{
           	 	 jine=$("#jiage").val();
           	 }
           	    if(parseInt($(this).val())!=0){
           	    	sn++;
                   var pa=parseInt($(this).val());
                   var guige=$(this).prev().prev().prev().prev().val();//规格id
                
                   if(sn==1){
                   	 myshook+='{"id":'+guige+',"saleNum":'+pa+',"goodsType":'+1+',"zbDirectSeedingId":'+null+',"packRate":'+jine+'}';
                   }else{
                   	 myshook+=',{"id":'+guige+',"saleNum":'+pa+',"goodsType":'+1+',"zbDirectSeedingId":'+null+',"packRate":'+jine+'}';
                   }
           	    }
           });
           myshook+=']';
					 if(myshook.length<10){
						// console.log("未添加商品够买数量");
					 }else{
							if(id!=""){
								$.ajax({
									url: urlcore + '/api/goodsCar/updateCar2?userId='+uuuuid+"&goodsId="+id,
									type: "post",
									contentType: "application/json;charset=utf-8",
									data: JSON.stringify({
										"myStocks": myshook,
									}),
									dataType: "json",
									success: function(data) {
										if(data.success == true) {
										   window.location.href = "11.0.0gouwuche.html";
										} else if(data.code == 'OVERTIME') {
											var thisUrl = window.location.href;
											if(thisUrl.indexOf('login.html') <= -1) {
												top.window.location.href = "1.0.0denglu.html";
											}
										} else {
											alert(data.msg);
										}
									},
									error: function() {
										alert("error");
									}
							  });
						  }
 	       }
			}else{
				window.location.href = "11.0.0gouwuche.html";
			}
}
//减法 
function jianhao(ts){
	var t=$(ts).next(); 
    if(t.val()!=''&&t.val()>0){
    	 t.val(parseInt(t.val())-1);
    }else{
    	 t.val(0);
    }
    zhijin(t.val());
}
// 加法
function jiahao(ts){
	var t=$(ts).prev();
	var shuju=$(ts).prev().prev().prev().prev();
	console.log(t.val(),shuju.val());
	if(parseInt(t.val())<parseInt(shuju.val())){
		t.val(parseInt(t.val())+1);
	}else{
		t.val(t);
	}
	zhijin(t.val());
}
//文本框 
 function wenbenkuang(ts){
    var shu=$(ts);
   	var shuju=$(ts).prev().prev().prev();
   	var results=/^(10|[1-9])$/.test(shu.val());//?"正":"非正";
		console.log(shu.val(),shuju.val(),results)
   	if(parseInt(shu.val())>parseInt(shuju.val())){
   		shu.val(shuju.val());
   	}else if(parseInt(shu.val())<=0 || shu.val()==""){
   		shu.val(0);
   	}else if(results==true){
       zhijin(shu.val());
   	}else{
			zhijin(shu.val());
		}
		
}
 
function zhijin(tva){
	        var zhizhi=0;
	    $("input[class='zhizhi']").each(function () {
	            zhizhi+=parseInt($(this).val());
        });
	   $("#goodsguigess").html("");
	   $("#hejijiage").html("");
	   
    var jiage=$("#jiage").val();
    var pifajiage=$("#pifajiage").val();
    
    $("#goodsguigess").html(zhizhi);
    if(zhizhi<=pifa){
    	 $("#hejijiage").html((zhizhi*pifajiage).toFixed(2));
    }else{
    	$("#hejijiage").html((zhizhi*jiage).toFixed(2));
    }
}







function shangchushangping(sizeidsss) {
		$.ajax({
			url: urlcore + "/api/goodsCar/delGoodsCarByGoodsIdOnPC?ids=" + sizeidsss,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				if(data.success == true) {
                   window.location.href="11.0.0gouwuche.html";
				}
				/*console.log(goodslist);*/
			},
			error: function() {
				alert("error");
			}
		});
	

}
